package dao

import (
	"gorm.io/gorm"
	"ruoyi-hello/app/domain"
	"ruoyi-hello/app/sys/model"
	"ruoyi-hello/config"
	"ruoyi-hello/utils/error"
)

var TableSysDictType = "sys_dict_type"

func DictTypePage(sqlQuery domain.SearchTableDataParam, isPage bool) (domain.TableDataInfo, error.BusstionError) {

	pageSize := sqlQuery.PageSize
	pageNum := sqlQuery.PageNum
	offset := (pageNum - 1) * pageSize
	var total int64
	var content []model.SysDictType
	var db *gorm.DB
	if isPage {
		err := config.MysqlConn.Primary.Table(TableSysDictType).Count(&total)
		if err.Error != nil {
			return domain.TableDataInfo{}, error.BusstionError{Code: error.SelectException.Code, Msg: "查询数据库异常，请稍后重试", Err: err.Error}
		}
		db = config.MysqlConn.Primary.Table(TableSysDictType).Limit(pageSize).Offset(offset)

	}
	err := db.Find(&content)
	if err.Error != nil {
		return domain.TableDataInfo{}, error.BusstionError{Code: error.SelectException.Code, Msg: "查询数据库异常，请稍后重试", Err: err.Error}
	} else {
		return domain.Success(content, total), error.Success()
	}
}
